﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing; 
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
using System.Data.OleDb;
using com.YiXiong.XML;

namespace QYWebSiteBN
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            cbaccess_connmode.SelectedIndex = 0;
            cbaccess_version.SelectedIndex = 0;
            cbmssql_connmode.SelectedIndex = 0;
            cbmssql_version.SelectedIndex = 0;
            cbMysql_connModel.SelectedIndex = 0;
            cbMysql_version.SelectedIndex = 0;
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnMysqlOK_Click(object sender, EventArgs e)
        {
            string connStr = String.Format("server={0};user id={1}; password={2};  pooling=false;port=3306",txtMysql_host.Text,txtMysql_username.Text,txtMysql_pass.Text);
            MySqlConnection myConn = new MySqlConnection(connStr);
            MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connStr);
            conn.Open();
            if (conn.State.ToString() == "Open")
            {
                string file = System.IO.Directory.GetCurrentDirectory();
             
                XmlAttributeCollection server = new XmlAttributeCollection();
                server.Add(new XmlAttribute("Name", txtMysql_host.Text));
                server.Add(new XmlAttribute("Port", "3360"));             
                XmlAttributeCollection dababase = new XmlAttributeCollection();
                dababase.Add(new XmlAttribute("Name", cbMysql_db.Text));
                dababase.Add(new XmlAttribute("User", cbMysql_db.Text));
                dababase.Add(new XmlAttribute("Password", cbMysql_db.Text));




                XMLHelper xml = new XMLHelper(@"c:\db.xml");
                xml.CreateRootNode("Databases");
                if (!xml.ExistNode("Databases/MySQL"))
                {
                xml.AddNode("Databases", "MySQL", string.Empty, null);
                }

                if (!xml.ExistNode(string.Format("Databases/MySQL/Server[@Name='{0}']", txtMysql_host.Text)))
                {
                    xml.AddElement("Databases/MySQL", "Server", "", server);
                }
                xml.AddElement("Databases/MySQL/Server[@Name='" + txtMysql_host.Text + "']", "Database", "", dababase);

               // xml.AddElement("WebSites/WebSite[@Name='ASP.NET']", "Task", "Get", ac);
                xml.Save();
              
            }
            else
            {
                MessageBox.Show("测试失败!");
            }

            conn.Close();

          
        }

        private void btnMysqlGetDB_Click(object sender, EventArgs e)
        {


            Queue<string> list= GetDataBaseListForMysql(txtMysql_host.Text, txtMysql_username.Text,txtMysql_pass.Text);
            foreach (string s in list)
            {
                cbMysql_db.Items.Add(s);
            }


        }


        public Queue<string> GetDataBaseListForMysql(string ip, string username, string password)
        {
            string connStr = String.Format("server={0};user id={1}; password={2}; pooling=false;port=3306", ip, username, password);

            Queue<string> list = new Queue<string>();

            try
            {

                using (MySqlConnection myConnection = new MySqlConnection(connStr))
                {
                    MySqlCommand myCommand = new MySqlCommand("show databases;", myConnection);
                    myConnection.Open();
                    MySqlDataReader myReader;
                    myReader = myCommand.ExecuteReader();

                    while (myReader.Read())
                    {
                        list.Enqueue(myReader.GetString(0));
                    }

                    myReader.Close();
                }
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.Message);
            }

            return list;
        }


        public Queue<string> GetTableListForAccess(string filepath)
        {
            Queue<string> list = new Queue<string>();
            string connectionString = string.Format("Provider=Microsoft.Jet.OleDb.4.0;Data Source={0};", filepath);  
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                connection.Open();
                System.Data.DataTable table = connection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
                foreach (System.Data.DataRow drow in table.Rows)
                {
                    string TableName = drow["Table_Name"].ToString();
                    list.Enqueue(TableName);

                }
            }

            return list;

        }


        public Queue<string> GetDataBaseListForMssql(string ip, string username, string password)
        {
            string constring = "Data Source={0};Persist Security Info=True;User ID={1};Password={2};";

            constring = string.Format(constring, ip, username, password);

            string sql = "Select Name FROM Master..SysDatabases order by Name";

            Queue<string> list = new Queue<string>();

            using (SqlConnection conn = new SqlConnection(constring))
            {
                try
                {
                    SqlCommand command = new SqlCommand(sql, conn);

                    conn.Open();

                    SqlDataReader reader = command.ExecuteReader();



                    while (reader.Read())
                    {
                        list.Enqueue(reader.GetString(0));

                    }
                }
                catch (SqlException e)
                {
                    MessageBox.Show("连接失败!");
                }
            }

            return list;

        }

        private void btnMSSQLGetDB_Click(object sender, EventArgs e)
        {

            Queue<string> list = GetDataBaseListForMssql(txtmssql_host.Text, txtmssql_username.Text, txtmssql_pass.Text);
            foreach (string s in list)
            {

                cbmssql_db.Items.Add(s);
                //cbMysql_db.Items.Add(s);
            }
        }

        private void btnAccessGetFilename_Click(object sender, EventArgs e)
        {
          OpenFileDialog ofd = new OpenFileDialog();
          ofd.Filter = "Access 数据库文件(*.mdb)|*.mdb";
          if (ofd.ShowDialog() == DialogResult.OK)
          {
              if (System.IO.File.Exists(ofd.FileName))
              {
                  txtaccess_dbfile.Text = ofd.FileName;
              }
          }
        }

        private void btnAccessOK_Click(object sender, EventArgs e)
        {
             Queue<string> list= GetTableListForAccess(txtaccess_dbfile.Text);
             return;
        }



        

    }
}
